package com.max.mil.service;

import java.util.List;
import java.util.Random;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.max.mil.common.jdbc.AdvanRowProcessor;
import com.max.mil.common.jdbc.DBToolKit;
import com.max.mil.common.util.DataPage;
import com.max.mil.model.GraContent;
import com.max.mil.share.GraContentService;

public class GraContentServiceImpl implements GraContentService
{

	private static  QueryRunner run = new QueryRunner(DBToolKit.getDataSource());
	
	
	@Override
	public int add(GraContent graContent) throws Exception
	{
		String sql = "INSERT INTO gra_content (NAME,CONTENT,REMARK,CREATOR,MODIFIER,CREATE_DATE,MODIFY_DATE) VALUES (?,?,?,?,?,?,?)";
		run.update(sql, graContent.getName(),graContent.getContent(),graContent.getRemark(),
				graContent.getCreator(),graContent.getModifier(),graContent.getCreateDate(),graContent.getModifyDate());
		Thread.sleep(100);
		int i = ((Long)run.query("SELECT LAST_INSERT_ID() AS id", new ScalarHandler("id"))).intValue();
	   
		return i;
	}

	@Override
	public GraContent get(int id) throws Exception
	{
		GraContent graContent = (GraContent) run.query("select * from GRA_CONTENT where id=?", 
				new BeanHandler(GraContent.class, new AdvanRowProcessor()), id);
		
		return graContent;
	}
	
	@Override
	public DataPage<GraContent> get(int pageNo, int pageSize) throws Exception
	{
		List<GraContent> list = null;
		
		int totalCount = ((Long)run.query("SELECT count(1) FROM gra_content ORDER BY id DESC", new ScalarHandler(1))).intValue();
		list = (List<GraContent>)run.query("SELECT * FROM gra_content ORDER BY id DESC LIMIT ?,?", 
				new BeanListHandler(GraContent.class, new AdvanRowProcessor()), (pageNo-1)*pageSize, pageSize);
		
		return new DataPage(list, pageNo, pageSize, totalCount);
	}

	@Override
	public List<GraContent> getRand(int size) throws Exception 
	{
		List<GraContent> list = null;
		DataPage<GraContent> dp = get(1,size);
		
		if(dp != null && dp.getTotalRecord()>0)
		{
			int pageCount = dp.getPageCount();
			Random random = new Random();
			dp = get(random.nextInt(pageCount)+1,size);
			
			list = dp.getList();
		}
		
		return list;
	}
}

/**
DROP TABLE IF EXISTS GRA_CONTENT;
CREATE TABLE GRA_CONTENT
(
   ID                   INTEGER(18) NOT NULL AUTO_INCREMENT COMMENT '主键',
   NAME                 VARCHAR(128) COMMENT '名称',
   CONTENT              VARCHAR(4096) COMMENT '内容',
   REMARK               VARCHAR(4096) COMMENT '备注',
   CREATOR              VARCHAR(128) COMMENT '创建者',
   MODIFIER             VARCHAR(128) COMMENT '更新者',
   CREATE_DATE          DATETIME COMMENT '创建时间',
   MODIFY_DATE          TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '最后更新时间',
   PRIMARY KEY (ID)
)
COMMENT='GRA内容表' ENGINE=MYISAM DEFAULT CHARSET=UTF8;
*/
